#include "tools.h"
#include <cstring>
void clear_screen()
{
	system("clear");
}

void clear()
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;
}

void anykey_continue()
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;
	puts("按任意键继续...");
	getch();
}

string translate(char c)
{
	switch(c)
	{
		case 'm': return "男";break;
		case 'w': return "女";break;
		default: return "未定义";break;
	}	
	return "未定义";
}

int get_cmd(vector<string>& str)
{
	int cur = 0;
	while(1)
	{
		clear();
		clear_screen();
		for(int i=0;i<str.size();i++)
		{
			if(cur == i)
			{
				cout << "->" << "【" << str[i] << "】" << endl;
			}
			else
			{
				cout << "  " << "【" << str[i] << "】" << endl;
			}
		}
		switch(getch())
		{
			case 183:if(cur>0) cur--; break;
			case 184:if(cur<str.size()-1) cur++; break;
			case 185:cur = str.size()-1; break;
			case 186:cur = 0; break;
			case 10:return cur; break;
			default: break;
		}
	}
}
char* get_str(char* str,int len,bool isdis)
{
	int pd = 0;
	char c;
	bool flag=false;
	clear();
	while(true)
	{
		clear();
		c = getch();
		if(9 == c)
		{
			if(!isdis)
			{
				for(int i=0;i<pd;i++)
				{
					printf("\b \b");
				}
				for(int i=0;i<pd;i++)
				{
					printf("%c",str[i]);
				}
				isdis = true;
				continue;
			}
			else
			{
				for(int i=0;i<pd;i++)
				{
					printf("\b \b");
				}
				for(int i=0;i<pd;i++)
				{
					printf("*");
				}
				isdis = false;
				continue;
			}
		}
		if(127 == c&&pd!=0)
		{
			if(pd == len)
			{
				flag=false;
			}
			//if(!isdis)
			printf("\b \b");
			str[pd] = '\0';
			pd--;
		}
		else if(!flag&&c!=10)
		{
			if(isdis)
			printf("%c",c);
			else
			printf("*");
			str[pd++] = c;
		}
		if(pd == len)
		{
			flag = true;
		}
		if(c==10)
		{
			break;
		}
	}
	str[pd] = '\0';
	printf("\n");
	clear();
	
	return str;
}
